﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides the approximation of the partial derivative of a specified function 
    /// by using a small limiting value.
    /// </summary>
    [Serializable]
    public class ComplexPartialDerivativeApproximator
    {
        /// <summary>
        /// The source function for the to approximate derivative.
        /// </summary>
        private HardMultivariateComplexFunction sourceFunction;

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPartialDerivativeApproximator"/> class.
        /// </summary>
        /// <param name="sourceFunction">The source function for the to approximate derivative.</param>
        public ComplexPartialDerivativeApproximator(HardMultivariateComplexFunction sourceFunction)
        {
            this.sourceFunction = sourceFunction;
        }

        /// <summary>
        /// Gets or sets the source function for the to approximate derivative.
        /// </summary>
        /// <value>The source function for the to approximate derivative.</value>
        public HardMultivariateComplexFunction SourceFunction
        {
            get { return sourceFunction; }
            set { sourceFunction = value; }
        }

        /// <summary>
        /// Approximates the specified complex function derivative by using a small limiting
        /// value 1e-5.
        /// </summary>
        /// <param name="x">The vector x at which the function derivative should be calculate.</param>
        /// <returns>
        /// The specified slope at the positions of x.
        /// </returns>
        public GeneralComplexVector LimitingValueApproximation(GeneralComplexVector x)
        {
            return this.LimitingValueApproximation(x, 1e-5);
        }

        /// <summary>
        /// Approximates the specified complex function derivative by using a small limiting
        /// value 1e-5.
        /// </summary>
        /// <param name="x">The vector x at which the function derivative should be calculate.</param>
        /// <param name="epsilon">The epsilon of the limiting value.</param>
        /// <returns>
        /// The specified slope at the positions of x.
        /// </returns>
        public GeneralComplexVector LimitingValueApproximation(GeneralComplexVector x, double epsilon)
        {
            if (x == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("x");
            }

            GeneralComplexVector result = new GeneralComplexVector(x.Count);
            ComplexNumber originalValue = this.sourceFunction.SolveAt(x);

            for (int i = 0; i < x.Count; i++)
            {
                x[i] += epsilon;
                result[i] = CalculateLimitingValue(originalValue, this.sourceFunction.SolveAt(x), epsilon);
                x[i] -= epsilon;
            }

            return result;
        }

        /// <summary>
        /// Approximates the specified real function derivative by using a small limiting value.
        /// </summary>
        /// <param name="fx">The first value f(x+h).</param>
        /// <param name="fxh">The second value f(x).</param>
        /// <param name="epsilon">The epsilon of the small limiting value.</param>
        /// <returns>
        /// The specified slope at the specified position x.
        /// </returns>
        private static ComplexNumber CalculateLimitingValue(ComplexNumber fx, ComplexNumber fxh, double epsilon)
        {
            return (fxh - fx) / epsilon;
        }
    }
}